/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package turtle.impl;

import java.util.Collection;
import java.util.LinkedList;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

import turtle.ActionBase;
import turtle.ActionFoward;
import turtle.ActionRotate;
import turtle.ActionSetPen;
import turtle.Choreography;
import turtle.Color;
import turtle.Farm;
import turtle.Stage;
import turtle.Turtle;
import turtle.TurtlePackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Stage</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link turtle.impl.StageImpl#getColor <em>Color</em>}</li>
 *   <li>{@link turtle.impl.StageImpl#getChoreographies <em>Choreographies</em>}</li>
 *   <li>{@link turtle.impl.StageImpl#getHeight <em>Height</em>}</li>
 *   <li>{@link turtle.impl.StageImpl#getWidth <em>Width</em>}</li>
 *   <li>{@link turtle.impl.StageImpl#getFarms <em>Farms</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class StageImpl extends EObjectImpl implements Stage {
	/**
	 * The cached value of the '{@link #getColor() <em>Color</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getColor()
	 * @generated
	 * @ordered
	 */
	protected Color color;

	/**
	 * The cached value of the '{@link #getChoreographies() <em>Choreographies</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getChoreographies()
	 * @generated
	 * @ordered
	 */
	protected EList<Choreography> choreographies;

	/**
	 * The default value of the '{@link #getHeight() <em>Height</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getHeight()
	 * @generated
	 * @ordered
	 */
	protected static final int HEIGHT_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getHeight() <em>Height</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getHeight()
	 * @generated
	 * @ordered
	 */
	protected int height = HEIGHT_EDEFAULT;

	/**
	 * The default value of the '{@link #getWidth() <em>Width</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getWidth()
	 * @generated
	 * @ordered
	 */
	protected static final int WIDTH_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getWidth() <em>Width</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getWidth()
	 * @generated
	 * @ordered
	 */
	protected int width = WIDTH_EDEFAULT;

	/**
	 * The cached value of the '{@link #getFarms() <em>Farms</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFarms()
	 * @generated
	 * @ordered
	 */
	protected EList<Farm> farms;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected StageImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return TurtlePackage.Literals.STAGE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetColor(Color newColor, NotificationChain msgs) {
		Color oldColor = color;
		color = newColor;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, TurtlePackage.STAGE__COLOR, oldColor, newColor);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setColor(Color newColor) {
		if (newColor != color) {
			NotificationChain msgs = null;
			if (color != null)
				msgs = ((InternalEObject)color).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - TurtlePackage.STAGE__COLOR, null, msgs);
			if (newColor != null)
				msgs = ((InternalEObject)newColor).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - TurtlePackage.STAGE__COLOR, null, msgs);
			msgs = basicSetColor(newColor, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, TurtlePackage.STAGE__COLOR, newColor, newColor));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Choreography> getChoreographies() {
		if (choreographies == null) {
			choreographies = new EObjectContainmentEList<Choreography>(Choreography.class, this, TurtlePackage.STAGE__CHOREOGRAPHIES);
		}
		return choreographies;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setHeight(int newHeight) {
		int oldHeight = height;
		height = newHeight;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, TurtlePackage.STAGE__HEIGHT, oldHeight, height));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setWidth(int newWidth) {
		int oldWidth = width;
		width = newWidth;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, TurtlePackage.STAGE__WIDTH, oldWidth, width));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Farm> getFarms() {
		if (farms == null) {
			farms = new EObjectResolvingEList<Farm>(Farm.class, this, TurtlePackage.STAGE__FARMS);
		}
		return farms;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void display() 
	{
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		String msg = "";		
		msg += "Dimensions du Stage: " + getHeight() + "h x " + getWidth() + "w\n";	
		EList<Choreography> choreographies =  getChoreographies();
		if (choreographies != null)
		{
			msg += "Choreographie(s): \n";
			EList<ActionBase> actions;
			
			for (Choreography choreographie : choreographies)
			{
				msg += "   * " + choreographie.getName() + " has these actions: \n";				
				actions = choreographie.getActions();
				
				for (ActionBase action : actions)
				{
					if (action instanceof ActionFoward)
						msg += "      + Foward: " + ((ActionFoward)action).getLength() + "\n";
					if (action instanceof ActionRotate)
						msg += "      + Rotate: " + ((ActionRotate)action).getAngle() + "angle(s)\n";
					if (action instanceof ActionSetPen)
						msg += "      + SetPen: " + ((ActionSetPen)action).getDirection() + "\n";
					msg += "        Turtle: " + action.getTarget().getName() + "\n";
				}
				
			}
		}
		System.out.println(msg);
	}
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case TurtlePackage.STAGE__COLOR:
				return basicSetColor(null, msgs);
			case TurtlePackage.STAGE__CHOREOGRAPHIES:
				return ((InternalEList<?>)getChoreographies()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case TurtlePackage.STAGE__COLOR:
				return getColor();
			case TurtlePackage.STAGE__CHOREOGRAPHIES:
				return getChoreographies();
			case TurtlePackage.STAGE__HEIGHT:
				return getHeight();
			case TurtlePackage.STAGE__WIDTH:
				return getWidth();
			case TurtlePackage.STAGE__FARMS:
				return getFarms();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case TurtlePackage.STAGE__COLOR:
				setColor((Color)newValue);
				return;
			case TurtlePackage.STAGE__CHOREOGRAPHIES:
				getChoreographies().clear();
				getChoreographies().addAll((Collection<? extends Choreography>)newValue);
				return;
			case TurtlePackage.STAGE__HEIGHT:
				setHeight((Integer)newValue);
				return;
			case TurtlePackage.STAGE__WIDTH:
				setWidth((Integer)newValue);
				return;
			case TurtlePackage.STAGE__FARMS:
				getFarms().clear();
				getFarms().addAll((Collection<? extends Farm>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case TurtlePackage.STAGE__COLOR:
				setColor((Color)null);
				return;
			case TurtlePackage.STAGE__CHOREOGRAPHIES:
				getChoreographies().clear();
				return;
			case TurtlePackage.STAGE__HEIGHT:
				setHeight(HEIGHT_EDEFAULT);
				return;
			case TurtlePackage.STAGE__WIDTH:
				setWidth(WIDTH_EDEFAULT);
				return;
			case TurtlePackage.STAGE__FARMS:
				getFarms().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case TurtlePackage.STAGE__COLOR:
				return color != null;
			case TurtlePackage.STAGE__CHOREOGRAPHIES:
				return choreographies != null && !choreographies.isEmpty();
			case TurtlePackage.STAGE__HEIGHT:
				return height != HEIGHT_EDEFAULT;
			case TurtlePackage.STAGE__WIDTH:
				return width != WIDTH_EDEFAULT;
			case TurtlePackage.STAGE__FARMS:
				return farms != null && !farms.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (height: ");
		result.append(height);
		result.append(", width: ");
		result.append(width);
		result.append(')');
		return result.toString();
	}

	
	public void addChoreography(Choreography choreography)
	{ 
		if (choreographies == null) {
			choreographies = new EObjectContainmentEList<Choreography>(Choreography.class, this, TurtlePackage.STAGE__CHOREOGRAPHIES);
		}
		choreographies.add(choreography);
	}
} //StageImpl
